package cn.wangxl.group.rpc.zookeeper;

import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.jboss.netty.util.CharsetUtil;

/**
 * @Description 自定义序列化工具（原装报错）
 * @Classname MyZkSerializer
 * @Created by wangxl
 * @Date 2020/6/27 17:44
 */
public class MyZkSerializer implements ZkSerializer {

//    @Override
//    public byte[] serialize(Object data) throws ZkMarshallingError {
//        return new byte[0];
//    }
//
//    @Override
//    public Object deserialize(byte[] bytes) throws ZkMarshallingError {
//        return null;
//    }

    /**
     * 序列化，将对象转化为字节数组
     */
    @Override
    public byte[] serialize(Object obj) throws ZkMarshallingError {
        return String.valueOf(obj).getBytes(CharsetUtil.UTF_8);
    }

    /**
     * 反序列化，将字节数组转化为UTF_8字符串
     */
    @Override
    public Object deserialize(byte[] bytes) throws ZkMarshallingError {
        return new String(bytes, CharsetUtil.UTF_8);
    }
}
